home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / misc / macro_1_0.lha / Macro.c < prev    next >
C/C++ Source or Header  |  1992-08-09  |  9KB  |  330 lines

  1. /**************************************************************************
  2. *  NAME: Macro                                                 _          *
  3. *  FUNCTION: Record and reinsert a key sequence             _ //   _      *
  4. *  WRITTEN BY: Piero Filippin (omega@sabrina.unipd.dei.it)  \X/ _ //   _  *
  5. *                                                               \X/ _ //  *
  6. *  This program is Public Domain (donation pleased)                 \X/   *
  7. *                                                                         *
  8. **************************************************************************/
  9.  
  10. #ifndef BUFFER
  11. #define BUFFER 4000
  12. #endif
  13.  
  14. #define IDLE  FALSE
  15. #define RECORDING TRUE
  16. #define QUALIFIER 0x19 /* LALT LSHIFT CTRL */
  17. #define START 0x1A /* [ */
  18. #define STOP 0x1B /* ] */
  19. #define INSERT 0x17 /* i */
  20. #define CLEAR 0x12 /* e */
  21. #define QUIT 0x10 /* q */
  22. #define INTUINAME "intuition.library"
  23. #define QUITSG (1<<data.quit)
  24. #define STARTSG (1<<data.startrec)
  25. #define STOPSG (1<<data.stoprec)
  26. #define INSERTSG (1<<data.insert)
  27. #define CLEARSG (1<<data.clear)
  28. #define STORESG (1<<data.mymsgport->mp_SigBit)
  29.  
  30. #define TITLE           "MACRO v1.1 - By Piero Filippin"
  31. #define IDLEMSG         "MACRO Status: Idle"
  32. #define RECORDINGMSG    "MACRO Status: Recording..."
  33. #define CLEARMSG        "MACRO Status: Buffer cleared"
  34. #define ENDRECORDINGMSG "MACRO Status: End recording"
  35. #define QUITMSG         "MACRO Status: Quitting..."
  36. #define INSERTMSG       "MACRO Status: Codes inserted"
  37. #define FULLMSG         "WARNING: Buffer FULL"
  38. #define NOMEMMSG        "Could NOT allocate memory!"
  39.  
  40. /* Declarations for CBACK */
  41. extern BPTR _Backstdout;         /* standard output when run in background */
  42. #ifdef DEBUG
  43. long _BackGroundIO = 1;          /* Flag to tell it we want to do I/O      */
  44. char string[11];
  45. #else
  46. long _BackGroundIO = 0;          /* Flag to tell it we don't want to do I/O*/
  47. #endif
  48. long _stack = 4000;              /* Amount of stack space our task needs   */
  49. char *_procname = "MACRO";       /* The name of the task to create         */
  50. long _priority = 1;              /* The priority to run us at              */
  51.  
  52. /* Funzioni */
  53. #ifdef DEBUG
  54. extern void Print(char *string,BOOL newline); /* Print string to _Backstdout */
  55. extern char *ltoh(char *string,long val);
  56. #endif
  57. extern void RawInsert(long code,long qualifier);
  58. extern void RemoveHandler(void);
  59. extern void InstallHandler(void);
  60.  
  61. void main(void);
  62.  
  63. struct handlerdata { 
  64.     struct MsgPort *mymsgport;
  65.     struct Task *thistask;
  66.     short quit;
  67.     short startrec;
  68.     short stoprec;
  69.     short insert;
  70.     short clear;
  71.     short store;
  72. };
  73.  
  74. struct RawMsg {
  75.     struct Message message;
  76.     UWORD code;
  77.     UWORD qualifier;
  78. };
  79.     
  80. struct MsgPort *inputDevPort;
  81. struct Interrupt handlerStuff;
  82. struct IOStdReq *inputRequestBlock;
  83. struct handlerdata data;
  84.  
  85. #ifdef INFOWINDOW
  86. struct IntuitionBase *IntuitionBase;
  87. struct Window *MyWindow;
  88. struct NewWindow NewWindow = {
  89.     100, /*LeftEdge   */
  90.     50,  /*TopEdge    */
  91.     300, /*Width      */
  92.     11,  /*Height     */
  93.     0,   /*DetailPen  */
  94.     1,   /*BlockPen   */
  95.     NULL,/*IDCMPFlags */
  96.     SMART_REFRESH | WINDOWDRAG | WINDOWDEPTH,/*Flags      */
  97.     NULL,/*FirstGadget*/
  98.     NULL,/*CheckMark  */
  99.     (UBYTE *)TITLE,/*Title      */
  100.     NULL,/*Screen     */
  101.     NULL,/*BitMap     */
  102.     0,   /*MinWidth   */
  103.     0,   /*MinHeight  */
  104.     0,   /*MaxWidth   */
  105.     0,   /*MaxHeight  */
  106.     WBENCHSCREEN/*Type*/
  107. };
  108. #endif
  109.  
  110. void main(void) {
  111.     short status = IDLE;
  112.     long bitmask = NULL;
  113.     char *buff;
  114.     UWORD *pointer;
  115.     struct RawMsg *myrawmsg;
  116.  
  117.     #ifdef INFOWINDOW
  118.     if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary(INTUINAME,0))) {
  119.         #ifdef DEBUG
  120.         Print("Could NOT open the Intuition Library!",TRUE);
  121.         #endif
  122.         Exit(20);
  123.     }
  124.   if(!(MyWindow = (struct Window *) OpenWindow( &NewWindow ))) {
  125.         CloseLibrary((struct Library*) IntuitionBase );
  126.         #ifdef DEBUG
  127.         Print("Could NOT open the Window!",TRUE);
  128.         #endif
  129.         Exit(30);  
  130.     }
  131.     #endif
  132.  
  133.     data.quit     = AllocSignal(-1);
  134.     data.startrec = AllocSignal(-1);
  135.     data.stoprec  = AllocSignal(-1);
  136.     data.insert   = AllocSignal(-1);
  137.     data.clear    = AllocSignal(-1);
  138.     data.thistask=FindTask(NULL);    
  139.  
  140.     data.mymsgport=CreatePort(0,0);
  141.  
  142.     if (!(buff= AllocMem(BUFFER-(BUFFER % 4),MEMF_CLEAR))) {
  143.         #ifdef DEBUG
  144.         Print(NOMEMMSG,TRUE);
  145.         #endif
  146.         #ifdef INFOWINDOW
  147.         SetWindowTitles(MyWindow,NOMEMMSG,(char *)-1);
  148.         Delay(1*50);
  149.         #endif
  150.         goto quit;
  151.     }
  152.  
  153.     #ifdef DEBUG
  154.     else {
  155.         Print("MACRO: allocated a ",FALSE);    
  156.         Print(ltoh(string,(long)( BUFFER-(BUFFER % 4) ) ),FALSE);
  157.         Print(" bytes long memory block at ",FALSE);
  158.         Print(ltoh(string,(long)buff),TRUE);
  159.     }
  160.     #endif
  161.  
  162.     pointer=(UWORD *)buff;
  163.     
  164.     InstallHandler();
  165.  
  166.     while ( !(bitmask & (1<<data.quit)) ) {
  167.         bitmask = Wait(QUITSG | STARTSG | STOPSG | INSERTSG | CLEARSG |STORESG );
  168.         if (bitmask & STORESG) {
  169.             while (myrawmsg=(struct RawMsg *)GetMsg(data.mymsgport)) {
  170.                 #ifdef DEBUG
  171.                 Print("Keycode: ",FALSE);
  172.                 Print(ltoh(string,(long)myrawmsg->code),FALSE);
  173.                 Print(" - Qualifier: ",FALSE);
  174.                 Print(ltoh(string,(long)myrawmsg->qualifier),TRUE);
  175.                 #endif
  176.                 if ((status == RECORDING) &&(myrawmsg->code != NULL)) {
  177.                     *(pointer++)=myrawmsg->code;
  178.                     *(pointer++)=myrawmsg->qualifier;
  179.                     #ifdef DEBUG
  180.                     Print("Keypress stored\nBuffer usage: ",FALSE);
  181.                     Print(ltoh(string,(long)((long)pointer - (long)buff)),FALSE);
  182.                     Print(" Buffer storage capacity: ",FALSE);
  183.                     Print(ltoh(string,(long)(BUFFER-(BUFFER % 4))),TRUE);
  184.                     #endif
  185.                     if (pointer>=(UWORD *)buff+(BUFFER-(BUFFER % 4))) {
  186.                         status=IDLE;
  187.                         #ifdef DEBUG
  188.                         Print(FULLMSG,TRUE);
  189.                         #endif
  190.                         #ifdef INFOWINDOW
  191.                         SetWindowTitles(MyWindow,FULLMSG,(char *)-1);
  192.                         Delay(1*50);
  193.                         #endif
  194.                     }
  195.                 }
  196.                 else{
  197.                     #ifdef INFOWINDOW
  198.                     SetWindowTitles(MyWindow,IDLEMSG,(char *)-1);
  199.                     #endif
  200.                 }
  201.             FreeMem(myrawmsg,sizeof(struct RawMsg));
  202.             }
  203.         }
  204.  
  205.         if (bitmask & CLEARSG) {
  206.             #ifdef DEBUG 
  207.             Print(CLEARMSG,TRUE);
  208.             #endif
  209.             pointer=(UWORD *)buff;
  210.             #ifdef INFOWINDOW
  211.             SetWindowTitles(MyWindow,CLEARMSG,(char *)-1);
  212.             #endif
  213.         }
  214.  
  215.         if ((bitmask & STARTSG) && (status == IDLE)) {
  216.  
  217.             #ifdef DEBUG
  218.             Print(RECORDINGMSG,TRUE);
  219.             #endif
  220.  
  221.             status=RECORDING;
  222.  
  223.             #ifdef INFOWINDOW
  224.             SetWindowTitles(MyWindow,RECORDINGMSG,(char *)-1);
  225.             #endif
  226.  
  227.         }
  228.  
  229.         if ((bitmask & STOPSG) && (status == RECORDING)) {
  230.             #ifdef DEBUG
  231.             Print(ENDRECORDINGMSG,TRUE);
  232.             #endif
  233.  
  234.             status=IDLE;
  235.  
  236.             #ifdef INFOWINDOW
  237.             SetWindowTitles(MyWindow,ENDRECORDINGMSG,(char *)-1);
  238.             #endif
  239.         }
  240.  
  241.         if ((bitmask & INSERTSG) && (status == IDLE)) {
  242.             UWORD *tempptr = (UWORD *)buff;
  243.             #ifdef DEBUG
  244.             Print(INSERTMSG,TRUE);
  245.             #endif
  246.  
  247.             while (tempptr<pointer) {
  248.                 RawInsert(*(tempptr++),*(tempptr++));
  249.             }
  250.  
  251.             #ifdef INFOWINDOW
  252.             SetWindowTitles(MyWindow,INSERTMSG,(char *)-1);
  253.             #endif
  254.         }
  255.  
  256.     }
  257.  
  258.     #ifdef DEBUG
  259.     Print(QUITMSG,TRUE);
  260.     #endif
  261.  
  262.     #ifdef INFOWINDOW
  263.     SetWindowTitles(MyWindow,QUITMSG,(char *)-1);
  264.     Delay(2*50);
  265.     #endif
  266.  
  267.     RemoveHandler();
  268. quit:
  269.     while (myrawmsg=(struct RawMsg *)GetMsg(data.mymsgport)) {
  270.         FreeMem(myrawmsg,sizeof(struct RawMsg));
  271.     }
  272.     DeletePort(data.mymsgport);
  273.     FreeMem(buff,(BUFFER-(BUFFER % 4)));
  274.  
  275.     #ifdef INFOWINDOW
  276.   DisplayBeep( MyWindow->WScreen );
  277.     CloseWindow( MyWindow );
  278.     CloseLibrary((struct Library*) IntuitionBase );
  279.     #endif
  280.  
  281.     #ifdef DEBUG
  282.     Close(_Backstdout);
  283.     #endif
  284.  
  285.     Exit(0);
  286. }
  287.  
  288. struct InputEvent *myhandler(struct InputEvent *event,struct handlerdata *data) {
  289.     register struct InputEvent *ep, *lastevent;
  290.     register struct RawMsg *myrawmsg;
  291.     for (ep = event, lastevent = NULL; ep != NULL; ep = ep->ie_NextEvent) {
  292.         if (ep->ie_Class == IECLASS_RAWKEY) {
  293.             if (!(ep->ie_Qualifier == (QUALIFIER | IEQUALIFIER_RELATIVEMOUSE))) {
  294.                 myrawmsg=(struct RawMsg *)AllocMem(sizeof(struct RawMsg),MEMF_CLEAR);
  295.                 myrawmsg->code = ep->ie_Code;
  296.                 myrawmsg->qualifier = ep->ie_Qualifier;
  297.                 myrawmsg->message.mn_ReplyPort = NULL;
  298.                 PutMsg(data->mymsgport,(struct Message *)myrawmsg);
  299.             }
  300.             else {
  301.                 switch (ep->ie_Code) {
  302.                     case QUIT:
  303.                         Signal(data->thistask,1<<data->quit);
  304.                         break;
  305.                     case START:
  306.                         Signal(data->thistask,1<<data->startrec);
  307.                         break;
  308.                     case STOP:
  309.                         Signal(data->thistask,1<<data->stoprec);
  310.                         break;
  311.                     case INSERT:
  312.                         Signal(data->thistask,1<<data->insert);
  313.                         break;
  314.                     case CLEAR:
  315.                         Signal(data->thistask,1<<data->clear);            
  316.                         break;
  317.                     default:
  318.                         goto noremove;
  319.                         break;
  320.                 } 
  321.                 if (lastevent == NULL) event = ep->ie_NextEvent; /* Remove */
  322.                 else lastevent->ie_NextEvent = ep->ie_NextEvent; /* handled events */
  323.             }
  324. noremove: ;
  325.         }
  326.         else lastevent = ep;  
  327.     }
  328.     return(event);
  329. }
  330.